var api = require('../../../utils/httpUtil.js');
var dateUtils = require('../../../utils/dateUtils.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    quesList: [],
    dataIndex: ['一、', '二、', '三、', '四、', '五、', '六、'],
    numIndex: ['(1)', '(2)', '(3)', '(4)', '(5)', '(6)', '(7)', '(8)', '(9)', '(10)'],
    openId: app.globalData.openId
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      date: dateUtils.currentDate()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.loadQuestion();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  loadQuestion() {
    api.httpRequest(api.questionListAPI, {})
      .then(res => {
        if (res.code == 200) {
          this.setData({
            quesList: res.data
          })
        }
      })
      .catch(error => {
        wx.showToast({
          title: error.message,
          duration: 1000,
          mask: true,
          icon: 'none'
        })
      });
  },
  suggestionInput(e) {
    this.setData({
      suggestion: e.detail.value
    })
  },
  scoreClick(e) {
    const score = e.currentTarget.dataset.index;
    const labelId = e.currentTarget.dataset.label;
    const quList = this.data.quesList;
    quList.map(item => {
      if (item.children) {
        item.children.map(citem => {
          if (citem.labelId == labelId) {
            citem.score = score;
          }
        });
      }
    });
    this.setData({
      quesList: quList
    })
  },
  submitClick(e) {
    const quList = this.data.quesList;
    var scoreList = []
    if (this.checkForm()) {
      quList.map(item => {
        if (item.children) {
          item.children.map(citem => {
            scoreList.push({
              "labelId": citem.labelId,
              "score": Number(citem.score)
            });
          });
        }
      });
    }
    api.httpRequest(api.questionAddAPI, {
      surveyDate: this.data.date,
      remark: this.data.suggestion,
      scoreList: scoreList,
      openId: this.data.openId
    })
    .then(res => {
      wx.showToast({
        title: res.msg,
        duration: 1000,
        mask: true,
        icon: 'none',
        success: function () {
          if (res.code == 200) {
            setTimeout(function () {
              wx.navigateBack();
            }, 1000);
          }
        }
      })
    })
    .catch(error => {
      wx.showToast({
        title: error.message,
        duration: 1000,
        mask: true,
        icon: 'none'
      })
    });

  },
  checkForm() {
    const quList = this.data.quesList;
    quList.map(item => {
      if (item.children) {
        item.children.map(citem => {
          if (!citem.score) {
            wx.showToast({
              title: citem.description + '还未选择，请填写完整！',
              duration: 1000,
              mask: true,
              icon: 'none'
            })
            return false;
          }
        });
      }
    });
    return true;
  }
})